<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class Install extends Model{
	
	function Install(){
		parent::Model();
		/* 加载数据库维护类 */
		$this->load->dbforge();
	}
	
	function init(){
		/* 删除已有的表 */
		$this->del_tables_exist();
		/* 建立试题表 */
		$this->table_question();		
		/* 建立标签表 */
		$this->table_tag();
		/* 建立题型表 */
		$this->table_catalog();
		/* 建立试卷表 */
		$this->table_paper();
		/* 建立试卷-试题表 */
		$this->table_paper_question();
		/* 建立类别表 */
		$this->table_section();
		/* 建立页面表 */
		$this->table_page();
		/* 建立答案表 */
		$this->table_answer();
		/* 建立试题-答案表 */
		$this->table_question_answer();
		/* 建立用户表 */
		$this->table_user();
		/* 建立用户组表 */
		$this->table_group();
		/* 建立角色表 */
		$this->table_role();
		/* 建立用户组-角色表 */
		$this->table_group_role();
		/* 建立会话表 */
		$this->table_sessions();
		/* 建立用户状态表 */
		$this->table_user_status();
		/* 建立表 */
		/* 初始化数据 */
		$this->init_tables();
	}
	/* 建立表 */	
	function create_table($table_name,$fields){
		$this->dbforge->add_field('id');
		$this->dbforge->add_field($fields);
		$this->dbforge->create_table($table_name);
	}
	/* 删除已有的表 */	
	function del_tables_exist(){
		$tables = $this->db->list_tables();
		foreach ($tables as $table)
		{
			if ($this->db->table_exists($table))
			{
				$this->dbforge->drop_table($table);
			}
			else {
				// do nothing
			}
		}
	}
	/* 建立试题表 */	
	function table_question(){
		$fields = array('catalog_id' => array('type' => 'VARCHAR',
		'constraint' => '20',
		),
		'content' => array('type' => 'TEXT',
		'null' => true,
		),
		'posted' => array('type' => 'DATETIME',
		'null' => true,
		),
		'verified' => array('type' => 'INT',
		'constraint' => 1,
		'default' => 0,
		),
		);
		$this->create_table('question',$fields);
	}
	/* 建立答案表 */	
	function table_answer(){
		$fields = array('answer' => array('type' => 'TEXT',
		'null' => true,
		),
		'solution' => array('type' => 'TEXT',
		'null' => true,
		),
		'verified' => array('type' => 'INT',
		'constraint' => 1,
		'default' => 0,
		),
		);
		$this->create_table('answer',$fields);
	}
	/* 建立试题-答案表 */	
	function table_question_answer(){
		$fields = array('question_id' => array('type' => 'VARCHAR',
		'constraint' => '255',
		'null' => true,
		),
		'answer_id' => array('type' => 'VARCHAR',
		'constraint' => '255',
		'null' => true,
		),
		);
		$this->create_table('rel_question_answer',$fields);
	}
	/* 建立标签表 */	
	function table_tag(){
		$fields = array('image_url' => array('type' => 'VARCHAR',
		'constraint' => '255',
		'null' => true,
		),
		);
		$this->create_table('tag',$fields);
	}
	/* 建立题型表 */	
	function table_catalog(){
		$fields = array('name' => array('type' => 'VARCHAR',
		'constraint' => '20',
		'null' => true,
		),
		);
		$this->create_table('catalog',$fields);
	}
	/* 建立试卷表 */	
	function table_paper(){
		$fields = array('name' => array('type' => 'VARCHAR',
		'constraint' => '100',
		'null' => true,
		),
		'posted' => array('type' => 'DATETIME',
		'null' => true,
		),
		);
		$this->create_table('paper',$fields);
	}
	/* 建立试卷-试题表 */	
	function table_paper_question(){
		$fields = array('question_id' => array('type' => 'VARCHAR',
		'constraint' => '255',
		'null' => true,
		),
		'paper_id' => array('type' => 'VARCHAR',
		'constraint' => '255',
		'null' => true,
		),
		);
		$this->create_table('rel_paper_question',$fields);
	}
	/* 建立类别表 */	
	function table_section(){
		$fields = array('name' => array('type' => 'VARCHAR',
		'constraint' => '255',
		'null' => true,
		),
		'parent_id' => array('type' => 'VARCHAR',
		'constraint' => '20',
		'null' => true,
		),
		);
		$this->create_table('section',$fields);
	}
	/* 建立页面表 */	
	function table_page(){
		$fields = array('title' => array('type' => 'VARCHAR',
		'constraint' => '255',
		'null' => true,
		),
		'heading' => array('type' => 'VARCHAR',
		'constraint' => '255',
		'null' => true,
		),
		'content' => array('type' => 'TEXT',
		'null' => true,
		),
		'posted' => array('type' => 'DATETIME',
		'null' => true,
		),
		);
		$this->create_table('page',$fields);
	}
	/* 建立用户表 */
	function table_user(){
		$fields = array('username' => array('type' => 'VARCHAR',
		'constraint' => '20',
		'null' => true,
		),
		'password' => array('type' => 'VARCHAR',
		'constraint' => '50',
		'null' => true,
		),
		'group_id' => array('type' => 'VARCHAR',
		'constraint' => '20',
		'null' => true,
		),
		'first_name' => array('type' => 'VARCHAR',
		'constraint' => '20',
		'null' => true,
		),
		'last_name' => array('type' => 'VARCHAR',
		'constraint' => '20',
		'null' => true,
		),
		'email' => array('type' => 'VARCHAR',
		'constraint' => '20',
		'null' => true,
		),
		'id_card' => array('type' => 'VARCHAR',
		'constraint' => '20',
		'null' => true,
		),
		'cell_phone' => array('type' => 'VARCHAR',
		'constraint' => '20',
		'null' => true,
		),
		'created' => array('type' => 'DATETIME',
		'null' => true,
		),
		'modified' => array('type' => 'DATETIME',
		'null' => true,
		),
		'status' => array('type' => 'VARCHAR',
		'constraint' => '1',
		'null' => true,
		),
		);
		$this->create_table('user',$fields);
	}
	/* 建立用户组表 */
	function table_group(){
		$fields = array('name' => array('type' => 'VARCHAR',
		'constraint' => '50',
		'null' => true,
		),
		'description' => array('type' => 'VARCHAR',
		'constraint' => '255',
		'null' => true,
		),
		'created' => array('type' => 'DATETIME',
		'null' => true,
		),
		'modified' => array('type' => 'DATETIME',
		'null' => true,
		),
		);
		$this->create_table('group',$fields);
	}
	/* 建立角色表 */
	function table_role(){
		$fields = array('name' => array('type' => 'VARCHAR',
		'constraint' => '50',
		'null' => true,
		),
		'description' => array('type' => 'VARCHAR',
		'constraint' => '255',
		'null' => true,
		),
		);
		$this->create_table('role',$fields);
	}
	/* 建立用户组-角色表 */
	function table_group_role(){
		$fields = array('group_id' => array('type' => 'VARCHAR',
		'constraint' => '20',
		'null' => true,
		),
		'role_id' => array('type' => 'VARCHAR',
		'constraint' => '20',
		'null' => true,
		),
		);
		$this->create_table('rel_group_role',$fields);
	}
	/* 建立会话表 */
	function table_sessions(){
		$fields = array('session_id'=>array('type' => 'VARCHAR',
		'constraint' => '40',
		),
		'ip_address' => array('type' => 'VARCHAR',
		'constraint' => '16',
		'null' => true,
		),
		'ip_address' => array('type' => 'VARCHAR',
		'constraint' => '16',
		'null' => true,
		),
		'user_agent' => array('type' => 'VARCHAR',
		'constraint' => '50',
		'null' => true,
		),
		'last_activity' => array('type' => 'INT',
		'constraint' => '10',
		'null' => true,
		),
		'user_data' => array('type' => 'TEXT',
		'null' => true,
		),
		);
		$this->create_table('sessions',$fields);
	}
	/* 建立用户状态表 */
	function table_user_status(){
		$fields = array('name' => array('type' => 'VARCHAR',
		'constraint' => '50',
		'null' => true,
		),
		'description' => array('type' => 'VARCHAR',
		'constraint' => '255',
		'null' => true,
		),
		'created' => array('type' => 'DATETIME',
		'null' => true,
		),
		'modified' => array('type' => 'DATETIME',
		'null' => true,
		),
		);
		$this->create_table('user_status',$fields);
	}
	/* 初始化数据 */
	function init_tables(){
		$now = date("Y-m-d H:i:s",time());
		/*初始化分组数据*/
		$groups = array(
		'guest'=>array(
		'name'=>'Guest',
		'description'=>'Guest Group',
		'created'=> $now,
		),
		'user'=>array(
		'name'=>'User',
		'description'=>'User Group',
		'created'=> $now,	
		),	
		'administrator'=>array(
		'name'=>'Administrator',
		'description'=>'Administrator Group',
		'created'=> $now,	
		),	
		);
		foreach($groups as $group){
			$this->db->set($group);
			$this->db->insert('group');
		}
		/*初始化用户状态数据*/
		$status = array(
		'actived'=>array(
		'name'=>'Actived',
		'description'=>'Actived',
		'created'=> $now,
		),
		'locked'=>array(
		'name'=>'Locked',
		'description'=>'Be Locked',
		'created'=> $now,
		),	
		);
		foreach($status as $stat){
			$this->db->set($stat);
			$this->db->insert('user_status');
		}
	}
}

?>